<template>
  <a-card :bordered="false">
    <table-list ref="tableList" :config="pageConfig" :columns="columns" :url="tableUrl" :formConfig="formConfig">

    </table-list>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { PageListMixin } from '@/mixins/PageListMixin'
import { getAction } from '@/api/manage'
import TableList from '@views/auto-pages/table/TableList'

export default {
  name: 'auto-index',
  mixins: [PageListMixin],
  components: {
    TableList
  },
  data() {
    return {
      description: '',
      // 表头
      pageConfig:{},
      searchConfig: [
        {
          type: 'input',
          name: 'competingName',
          placeholder: '竞品名称'
        },
        {
          type: 'input',
          name: 'brand',
          placeholder: '竞品品牌'
        },
        {
          type: 'input',
          name: 'classifyName',
          placeholder: '所属系列'
        }
      ],
      columns: [
        {
          width: 50,
          type: 'checkbox',
        },
        {
          title: '创建人',
          minWidth: 80,
          field: 'createName'
        },
        {
          title: '创建时间',
          minWidth: 80,
          field: 'createTime'
        },
        {
          title: '更新人',
          align: 'left',
          minWidth: 80,
          field: 'updateName'
        },
        {
          title: '更新时间',
          minWidth: 80,
          field: 'updateTime'
        },
        {
          title: '状态',
          field: 'status',
          minWidth: 80,
          slots: { default: 'update' }
        },
        {
          title: '操作',
          minWidth: 80,
          fixed: 'right',
          slots: { default: 'action' }
        }
      ],
      formConfig: [],
      url: {
        config: window._CONFIG['domianURL'] + '/apiPageConfig/getPageconfigCurrent',
        detail: window._CONFIG['domianURL'] + '/pageconfig/cnPageconfigHead/getInfo',
      },
      tableUrl: {}
    }
  },
  async created() {
    await this.getConfig()
  },
  watch: {
    $route: {
      handler: 'getConfig'
    }
  },
  methods: {
    async getConfig() {
      this.tableUrl = {}
      this.columns = []
      this.$nextTick(() => {
        this.$refs.tableList.dataSource = []
      })
      let { functionCode, tableCode } = this.$route.meta
      let res = await getAction(this.url.config, { functionCode, tableCode })
      if (res.success) {
        this.handleData({...res.result,functionCode})
      }
    },
    handleData(data) {
      this.pageConfig = data
      let table = data.detailVoList.filter(item => item.tableShow == 1)
      let current = data.currentVos.map(item=>item.fieldCode)
      let tableColumns = table.map(item => Object.assign({
        title: item.fieldValue,
        field: item.fieldCode,
        minWidth: item.width || 80,
        fixed: item.fixed,
        sort: item.tableOrder,
        visible: current.includes(item.fieldCode),
      })).sort((a, b) => (a.sort || 9999) - (b.sort || 9999))
      tableColumns.unshift({
        width: 50,
        type: 'checkbox',
        visible: true,
      })
      tableColumns.push({
          title: '创建人',
          minWidth: 80,
          field: 'createName'
        },
        {
          title: '创建时间',
          minWidth: 80,
          field: 'createTime'
        },
        {
          title: '更新人',
          align: 'left',
          minWidth: 80,
          field: 'updateName'
        },
        {
          title: '更新时间',
          minWidth: 80,
          field: 'updateTime'
        },
        {
          title: '状态',
          field: 'status',
          minWidth: 80,
          slots: { default: 'update' }
        },
        {
          title: '操作',
          minWidth: 80,
          fixed: 'right',
          type: '',
          visible: true,
          slots: { default: 'action' }
        })
      this.columns = tableColumns
      data.configUrl.forEach(item => {
        this.tableUrl[item.interfaceTile] = window._CONFIG['domianURL'].split('/mdm').join('') + item.interfaceUrl
      })
      this.$refs.tableList.loadData()
      let formConfig = data.detailVoList.filter(item => item.formShow == 1)
        .map(item => Object.assign({}, {
          label: item.fieldValue,
          type: item.controlType,
          placeholder: item.placeholder || item.fieldValue,
          name: item.fieldCode,  // 后台接受字段
          sort: item.formOrder,
          dictCode: item.controlDictVal,
          dataType: item.controlDictVal,
          check: { rules: [{ required: item.isNeed, message: ' ' }] }
        })).sort((a, b) => (a.sort || 9999) - (b.sort || 9999))
      this.formConfig = formConfig

    },
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';
</style>